//
//  SettingsViewController.m
//  Fangmm
//
//  Created by 阮琦 on 16/3/16.
//  Copyright © 2016年 com.fangmm. All rights reserved.
//

#import "SettingsViewController.h"

#import "ChooseLoginModeViewController.h"
#import "NewRequirementViewController.h"
#import "PreNavigationViewController.h"
#import "MainNavigationViewController.h"
#import "MainIndexViewController.h"
#import "CallServiceViewController.h"
#import "WebViewController.h"
#import "SureLogoutViewController.h"

#import "Utility.h"
#import "Constants.h"
#import "ACMacros.h"
#import "AppDrawer.h"

#import <StoreKit/StoreKit.h>
#import <MobClick.h>

@interface SettingsViewController () <UITableViewDataSource, UITableViewDelegate, SKStoreProductViewControllerDelegate, LogOutDelegate>

@end

@implementation SettingsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configureNavigationBar];
    [self configureTableViews];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)viewWillAppear:(BOOL)animated
{
    [MobClick beginLogPageView:@"设置页"];
}
-(void)viewWillDisappear:(BOOL)animated
{
    [MobClick endLogPageView:@"设置页"];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark - Configuring the view’s layout behavior
- (void)configureNavigationBar {
    self.title = @"设置";
    self.navigationItem.hidesBackButton = true;
    UIImage *backImage = [[UIImage imageNamed:@"back-18-36"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc]initWithImage:backImage style:UIBarButtonItemStyleDone target:self action:@selector(clickToPopBack)];
    self.navigationItem.leftBarButtonItem = leftBarButton;
}

- (void)configureTableViews {
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, Main_Screen_Width, Main_Screen_Height)];
    [self.tableView setDelegate:self];
    [self.tableView setDataSource:self];
    [self.tableView setBounces:false];
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self.tableView setBackgroundColor:RGBACOLOR(240, 240, 240, 1)];
    [self.view addSubview:self.tableView];
    
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    switch (section) {
        case 0:
            return 1;
            break;
        case 1:
            return 1;
            break;
        case 2:
            return 4;
            break;
        case 3:
            return 1;
            break;
        default:
            return 0;
            break;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    [cell setSelectionStyle:UITableViewCellSelectionStyleGray];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, Main_Screen_Width - 20, 50)];
    [label setTextColor:RGBACOLOR(71, 62, 58, 1)];
    [label setFont:[UIFont systemFontOfSize:15]];
    [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
    [cell setTintColor:[UIColor groupTableViewBackgroundColor]];
    [cell addSubview:label];
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(10, 49.3, Main_Screen_Width - 10, 0.7)];
    [line setBackgroundColor:RGBACOLOR(204, 204, 204, 0.8)];
    [cell addSubview:line];
    NSInteger section = indexPath.section;
    NSInteger row = indexPath.row;
    switch (section) {
        case 0:
            [label setText:@"用户指南"];
            break;
        case 1:
            switch (row) {
                case 0:
                    [label setText:@"客服与帮助"];
                    break;
                default:
                    break;
            }
            break;
        case 2:
            switch (row) {
                case 0:
                    [label setText:@"给房妈妈好评"];
                    break;
                case 1:
                    [label setText:@"建议与反馈"];
                    break;
                case 2:
                    [label setText:@"法律条款"];
                    break;
                case 3:
                    [label setText:@"关于房妈妈"];
                    break;
                default:
                    break;
            }
            break;
        case 3:
            [label setText:@"退出登录"];
            break;
            
        default:
            break;
    }
    
    return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 4;
}

#pragma mark - Table view delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 15;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    [cell setSelected:true];
    NSInteger section = indexPath.section;
    NSInteger row = indexPath.row;
    WebViewController *webVC = [[WebViewController alloc]init];
    switch (section) {
        case 0:
            webVC.destinationUrl = @"http://m.fangmm.com/app/client/guide";
            webVC.titleString = @"用户指南";
            [self.navigationController pushViewController:webVC animated:true];
            break;
        case 1:
            switch (row) {
                case 0:
                {
                    CallServiceViewController *callServiceVC = [[CallServiceViewController alloc]initWithNibName:@"CallServiceViewController" bundle:nil];
                    [self presentViewController:callServiceVC animated:true completion:^{
                    }];
                }
                    break;
                default:
                    break;
            }
            break;
        case 2:
            switch (row) {
                case 0:
//                    [label setText:@"给房妈妈好评"];
                {
                    SKStoreProductViewController *storeProductViewContorller =[[SKStoreProductViewController alloc] init];
                    storeProductViewContorller.delegate = self;
                    [storeProductViewContorller loadProductWithParameters:
                     //appId唯一的
                     @{SKStoreProductParameterITunesItemIdentifier : @"1044131785"} completionBlock:^(BOOL result, NSError *error) {
                         //block回调
                         if(error){
                             NSLog(@"error %@ with userInfo %@",error,[error userInfo]);
                         }else{
                             [self presentViewController:storeProductViewContorller animated:true completion:^{
                                 
                             }];
                         }
                    }];
                }
                    break;
                case 1:
                    webVC.destinationUrl = @"http://m.fangmm.com/app/client/feedback";
                    webVC.titleString = @"建议与反馈";
                    [self.navigationController pushViewController:webVC animated:true];
                    break;
                case 2:
                    webVC.destinationUrl = @"http://m.fangmm.com/app/client/provision";
                    webVC.titleString = @"法律条款";
                    [self.navigationController pushViewController:webVC animated:true];
                    break;
                case 3:
                    webVC.destinationUrl = @"http://m.fangmm.com/app/client/about/fangmm";
                    webVC.titleString = @"关于房妈妈";
                    [self.navigationController pushViewController:webVC animated:true];
                    break;
                default:
                    break;
            }
            break;
        case 3:
        {
            SureLogoutViewController *sureLogoutVC = [[SureLogoutViewController alloc]initWithNibName:@"SureLogoutViewController" bundle:nil];
            [sureLogoutVC setDelegate:self];
            [self presentViewController:sureLogoutVC animated:true completion:^{
                
            }];
        }
            break;
            
        default:
            break;
    }
    [cell setSelected:false];
}
#pragma mark - Triggering Methods 
- (void)clickToPopBack {
    [self.navigationController popViewControllerAnimated:true];
}

-(void)productViewControllerDidFinish:(SKStoreProductViewController*)viewController{
    [self dismissViewControllerAnimated:YES completion:^{
    }];
}

- (void)clickToLogOut {
    [Utility clearObjectForKey:kToken];
    [Utility clearObjectForKey:kUser];
    [Utility clearObjectForKey:kRequirement];
    [Utility clearObjectForKey:kUserId];
    [MobClick profileSignOff];
    NSString *token = [Utility getObjectForKey:kToken];
    PreNavigationViewController *preNavigationVC;
    if (token == nil || [token isEqual:@""] || [token isEqual:kToken]) {
        ChooseLoginModeViewController *chooseLoginModelVC = [[ChooseLoginModeViewController alloc]initWithNibName:@"ChooseLoginModeViewController" bundle:nil];
        preNavigationVC = [[PreNavigationViewController alloc]initWithRootViewController:chooseLoginModelVC];
    }
    [UIApplication sharedApplication].keyWindow.rootViewController = preNavigationVC;
    [[UIApplication sharedApplication].keyWindow makeKeyAndVisible];
}
@end
